package com.xiaotu.easypoi.testexport.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.xiaotu.easypoi.testexport.model.CourseEntity;
import com.xiaotu.easypoi.testexport.model.ExportUserModel;
import com.xiaotu.easypoi.testexport.model.StudentEntity;
import com.xiaotu.easypoi.testexport.model.TeacherEntity;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangxiao
 * @create 2021-05-19 17:46
 */
@RestController
@CrossOrigin
public class ExportExcelController {

    /**
     * 导出用户表
     */
    @RequestMapping("exportExcel")
    public void exportExcel() {
        List<ExportUserModel> list = new ArrayList<>();
        ExportUserModel exportUser1 = new ExportUserModel("张三", 1, new Date());
        list.add(exportUser1);
        ExportUserModel exportUser2 = new ExportUserModel("李四", 0, new Date());
        list.add(exportUser2);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生", "学生"),
                ExportUserModel.class, list);
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\exportUser.xlsx"));
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 导出课程表
     */
    @RequestMapping("exportCourseExcel")
    public void exportCourseExcel() {
        List<StudentEntity> studentEntityList = new ArrayList<>();
        studentEntityList.add(new StudentEntity("1", "张三1", 1, new Date(), new Date(), "C:\\Users\\Administrator\\Desktop\\Jay图集\\无与伦比.jpg"));
        studentEntityList.add(new StudentEntity("2", "张三2", 2, new Date(), new Date(), "C:\\Users\\Administrator\\Desktop\\Jay图集\\周杰伦.jpg"));
        List<CourseEntity> courseEntityList = new ArrayList<>();
        courseEntityList.add(new CourseEntity("1", "海贼王必修(1)", new TeacherEntity("1", "老王0"), studentEntityList));
        courseEntityList.add(new CourseEntity("2", "海贼王必修(2)", new TeacherEntity("2", "老王0"), studentEntityList));
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("2412312", "测试"),
                CourseEntity.class, courseEntityList);
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\exportCourseExcel.xlsx"));
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
